/**
 * Copyright 2013 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2013-8-27 下午9:26:39
 */
package com.absir.android.goods;

import android.content.Context;
import android.content.SharedPreferences;

import com.absir.android.app.ApplicationBean;

/**
 * @author absir
 * 
 */
public abstract class CoinGoodsDefine extends GoodsDefine {

	/** coinCache */
	private int coinCache;

	/** sharedPreferences */
	private SharedPreferences sharedPreferences;

	/** COIN_NAME */
	public final static String COIN_NAME = "0";

	/**
	 * @return
	 */
	public SharedPreferences getSharedPreferences() {
		if (sharedPreferences == null) {
			sharedPreferences = ApplicationBean.getApplication().getSharedPreferences(CoinGoodsDefine.class.getName(), Context.MODE_PRIVATE);
			coinCache = sharedPreferences.getInt(COIN_NAME, getInitialCoinCount());
		}

		return sharedPreferences;
	}

	/**
	 * @return
	 */
	protected int getInitialCoinCount() {
		return 100;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.absir.android.goods.IGoodsDefine#getGoodsInfo(java.lang.Object)
	 */
	@Override
	public final IGoodsInfo getGoodsInfo(Object tag) {
		return tag instanceof Integer ? getGoodsInfo((int) (Integer) tag) : getGoodsInfo(tag.toString());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.absir.android.goods.IGoodsDefine#getGoodsInfoCount(java.lang.Object)
	 */
	@Override
	public final int getGoodsInfoCount(Object tag) {
		return tag instanceof Integer ? -1 : getGoodsInfoCount(tag.toString());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.absir.android.goods.IGoodsDefine#gainGoodsInfo(java.lang.Object,
	 * int)
	 */
	@Override
	public final boolean gainGoodsInfo(Object tag, int count) {
		return tag instanceof Integer ? gainGoodsCoin(count * (Integer) tag) : gainGoodsInfo(tag.toString(), count);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.absir.android.goods.IGoodsDefine#consumeGoodsInfo(java.lang.Object,
	 * int)
	 */
	@Override
	public final boolean consumeGoodsInfo(Object tag, int count) {
		return tag instanceof Integer ? consumeGoodsCoin(count * (Integer) tag) : consumeGoodsInfo(tag.toString(), count);
	}

	/**
	 * @return
	 */
	public int getGoodsCoin() {
		getSharedPreferences();
		return coinCache;
	}

	/**
	 * @param coin
	 */
	public void setGoodsCoin(int coin) {
		getSharedPreferences().edit().putInt(COIN_NAME, coin).commit();
	}

	/**
	 * @param name
	 * @return
	 */
	public int getGoodsInfoCount(String name) {
		return getSharedPreferences().getInt(name, 0);
	}

	/**
	 * @param name
	 * @param count
	 */
	public void setGoodsInfoCount(String name, int count) {
		getSharedPreferences().edit().putInt(name, count).commit();
	}

	/**
	 * @param coin
	 * @return
	 */
	public boolean gainGoodsCoin(int coin) {
		setGoodsCoin(getGoodsCoin() + coin);
		return true;
	}

	/**
	 * @param name
	 * @param count
	 * @return
	 */
	public boolean gainGoodsInfo(String name, int count) {
		setGoodsInfoCount(name, getGoodsInfoCount(name) + count);
		return true;
	}

	/**
	 * @param coin
	 * @return
	 */
	public boolean consumeGoodsCoin(int coin) {
		coin = getGoodsCoin() - coin;
		if (coin < 0) {
			return false;
		}

		setGoodsCoin(coin);
		return true;
	}

	/**
	 * @param name
	 * @param count
	 * @return
	 */
	public boolean consumeGoodsInfo(String name, int count) {
		count = getGoodsInfoCount(name) - count;
		if (count < 0) {
			return false;
		}

		setGoodsInfoCount(name, count);
		return true;
	}

	/**
	 * @param coin
	 * @return
	 */
	public abstract IGoodsInfo getGoodsInfo(int coin);

	/**
	 * @param name
	 * @return
	 */
	public abstract IGoodsInfo getGoodsInfo(String name);
}
